3* Project      :  The Full, Persistent, and Symmetric Pass-Through of a Temporary VAT Cut
* Author(s)    :  Márcia Pereira, João Quelhas, Tiago Bernardino, Ricardo Duque Gabriel
* Date         :  01/04/2025
* Description  :  Additional tables and figures
* Dependencies :
* Modifications:  (add date, author and change)

*********************************************************
*           Additional Tables and Figures               *
*********************************************************

clear all 
set more off
cap log close

global main_path "`c(pwd)'"

cd "$main_path"

global additional_data    "$main_path/additional_data"
global additional_figures "$main_path/additional_figures"
global additional_tables  "$main_path/additional_tables"

cap mkdir "$additional_figures"
cap mkdir "$additional_tables"

ssc install grstyle, replace
ssc install ftools, replace
ssc install reghdfe, replace
ssc install xtevent, replace

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 14
grstyle graphsize y 8
grstyle color background white

*********************************************************


* FIGURE 8: Event Study of Agricultural Wholesale Producer Prices
*********************************************************

* Reading all of the csv files.
local files: dir "$additional_data/producer_prices" files "*csv"

cap mkdir "$additional_data/producer_prices/intermediate"

foreach file of local files {
	
	* Load the csv file.
	import delimited "$additional_data/producer_prices/`file'", delimiter(";") encoding(UTF-8) clear 
	di "******* Reading `file'. *******"

	bys produto: gen n = _n
	
	local title = subinstr("`file'", ".csv", "", .)	
	save "$additional_data/producer_prices/intermediate/`title'.dta", replace
}

* Using the bovino csv as a base file.
use   "$additional_data/producer_prices/intermediate/cp01121-bovino", clear 
erase "$additional_data/producer_prices/intermediate/cp01121-bovino.dta"

* Category variable.
gen category = "cp01121-bovino.dta" 

* Appending all the remaining files.
local files: dir "$additional_data/producer_prices/intermediate" files "cp*"

foreach file of local files {
	append using "$additional_data/producer_prices/intermediate/`file'"
	replace category = "`file'" if category == ""
}

* Daily date.
gen day   = substr(data,1,2)
gen month = substr(data,4,2)
gen year  = substr(data,7,4)
destring day month year, replace
gen ddate = mdy(month,day,year)  // Creating a new date variable
format ddate %td
	
* Monthly date.
gen mdate = ym(year,month)
format mdate %tm
	
* Weekly date.
generate wdate = ddate
format wdate %tdCY!ww
	
* Sorting and ordering.     
sort produto mdate wdate ddate 
order mdate wdate ddate 

* ID variable.
bys produto: gen id_n = _n 
egen id = group(produto espcie regio mercado) 

* Treated vs. non-treated items.
global treated "Abóbora Alface Alfeiro Azeite Batata Cebola Cenoura Courgette Couve Ervilha Espinafre Farroupo Frango Laranja Leitão Maçã Melão Nabo Novilha Novilho Peru Perua Porca Porco Varrasco Pêra Tomate Vaca Vitela Vitelo Vitelão Ovo"
cap gen treatment = 0
foreach j of global treated {
	replace treatment = 1 if espcie == "`j'" | strpos(espcie, "`j'")
}

* Dropping if product not available for at least 2/3 of the year.
cap drop aux
bys produto espcie year: egen aux = count(freq)
local th = 2/3*52 // 52 = weeks in each year
drop if aux < `th' & year < 2024

* Dropping non-treated categories.
drop if category == "cp01163-frutos_secos.dta"
drop if category == "cp01123-ovino_e_caprino.dta"

* Collapsing by espécie and semana.
collapse (mean) freq treatment, by(category produto espcie id year wdate semana)
order id espcie produto  
sort id wdate 

* Getting rid of repeated time values within panel.
bys id wdate: gen test = _N
tab test 

* Price index (as not to average prices in kg and numbered quantities).
bys id: gen price_chg = freq/freq[_n-1]-1
bys id: gen index = 100 if _n==1
bys id: replace index = index[_n-1]*(1+price_chg) if _n>1

* Base week (to be adjusted)
global ann = 12 // 12 = 20/03, 13 = 27/03
global imp = 16 // 16 = 17/04, 17 = 24/04
global base = ${ann} 		// CHANGE HERE THE GLOBAL TO BE USED (ann OR imp)
global act "announcement"   // CHANGE HERE THE ACT (announcement OR implementation)
	
* Treatment window.
cap drop treat_window
gen treat_window = 0 
replace treat_window = 1 if year == 2023 & semana >= ${base}+1
cap bys id: gen count = _N
tab count

* Keeping those available within the treatment horizon.
cap drop policy
cap drop window 
cap drop aux
gen policy = 1 if (year == 2023 & semana == ${base}+1)
bys id: gen window = 0 if policy != .
forval i=1(1)20 {
	bys id: replace window = -`i' if policy[_n+`i']==1 
	bys id: replace window = `i'  if policy[_n-`i']==1 
}
bys id: egen aux = max(window)
tab aux

* Rebasing index to t=-1.
cap drop value_2023w${base} index_2023w${base}
bys id: egen value_2023w${base} = max(cond(year==2023 & semana==${base}, index, .))
gen index_2023w${base} = 100*index/value_2023w${base} 

* Event study without treatment and control groups for the announcement + implementation.
keep if aux == 20 
keep if year == 2023
bys id: gen time = _n

xtevent index_2023w${base}, policyvar(treat_window) panelvar(id) timevar(time) window(-3 7) cluster(id) impute(nuchange) diffavg reghdfe
 
xteventplot, ytitle("Percentage change") xtitle("Weeks to Event") ///
			 scatterplotopts(lcolor(dknavy) recast(connected) mcolor(dknavy) ///
			 msymbol(circle)) ciplotopts(recast(rarea) mcolor(black) fcolor(dknavy*0.2) lcolor(gray*0.5)) ///
			 graphregion(fcolor(white)) yscale(lcolor(black) lwidth(none) line) ///
			 tscale(lcolor(black) lwidth(none) line) ylabel(-60(20)20) ///
			 xline(0, lcolor("gray*0.5") lpattern(solid) lwidth(medthin)) ///
			 xline(4, lcolor("gray*0.5") lpattern(solid) lwidth(medthin)) ///
			 nosupt noprepval nopostpval /// 
			 text(18 0.1 "Announcement" 18 4.1 "Implementation", color("gray") placement(e)) 
			 
graph export "$additional_figures/figure_8.png", as(png) replace width(2400) height(1372)

*********************************************************


* FIGURE A.1: Headline and Food Inflation for Portugal
*********************************************************

// Importing data from Excel file.
import excel "$additional_data/headline_food_hicp.xlsx", sheet("hicp") firstrow clear

// Generating a monthly date variable.
gen monthly_date = mofd(date)

// Setting time series data with monthly frequency.
tsset monthly_date, monthly

// Multiplying certain variables by 100 for percentage representation.
replace headline_yoy = headline_yoy*100
replace food_yoy = food_yoy*100

// Creating a line graph to compare headline and food year-on-year percentage changes over time.
twoway (line headline_yoy date, lcolor("dknavy%100") lpattern(solid) lwidth(medthick)) || ///
       (line food_yoy date, lcolor("dkorange%100") lpattern(solid) lwidth(medthick)), ///
       ytitle("Year-on-year percentage change", size(medsmall)) ///
       ylabel(-5(5)20, labsize(medsmall)) yscale(lcolor(black) lwidth(none) line) ///
       tline(01/05/2023, lcolor(gray) lpattern(dash) lwidth(medthin)) ///
       xscale(lcolor(black) lwidth(none) line) ///
       ttitle("") tlabel(#16, labsize(medsmall) angle(forty_five) format(%tdMon-CCYY)) ///
       legend(order(1 "Headline HICP" 2 "Food HICP") rows(1) pos(6) region(lcolor(black)) size(medsmall)) plotregion(lcolor(black))

// Exporting the graph as a png file
graph export "$additional_figures/figure_a1.png", as(png) replace width(2400) height(1372)

*********************************************************


* FIGURE A.11: Search Intensity on Google for Value-Added Tax and VAT in Portugal
*********************************************************

// Importing data from Excel file.
import excel "$additional_data/google_searches.xlsx", sheet("google") firstrow clear

// Setting time series data with weekly frequency.
tsset week_graph

keep if week >= td(01/01/2023)
keep if week <= td(01/02/2024)

// Creating a line graph to compare search intensity over time.
twoway  (tsline TaylorSwift, lcolor("gs11") lpattern(solid) lwidth(medthick)) ///
	(tsline Impostosobreovaloracrescenta, lcolor("dknavy") lpattern(solid) lwidth(medthick)) ///
	(tsline IVA, lcolor("dkorange") lpattern(solid) lwidth(medthick)), ///
        ytitle("Measure of search intensity (maximum = 100)", size(medsmall)) ylabel(0(20)100, labsize(medsmall)) ///
	yscale(lcolor(black) lwidth(none) line) xscale(lcolor(black) lwidth(none) line) ///
        tline(29/03/2023 19/04/2023 25/10/2023 03/01/2024, lcolor(gray) lpattern(dash) lwidth(medthin)) ///
        ttitle("") tlabel(#14, labsize(medsmall) angle(forty_five) format(%tdMon-CCYY)) ///
        legend(order(2 "Imposto sobre valor acrescentado" 3 "IVA" 1 "Taylor Swift") rows(1) pos(6) region(lcolor(black)) size(medsmall)) plotregion(lcolor(black))

// Exporting the graph.
graph export "$additional_figures/figure_a11.png", as(png) replace width(2400) height(1372)

*********************************************************


* FIGURE A.12: Inflation Expectations over the next 12 months
*********************************************************

// Importing data from Excel file.
import excel "$additional_data/inflation_expectations.xlsx", sheet("inflation_expectations") firstrow clear

gen number = _n

// Setting time series data with weekly frequency.
tsset number

// Creating a line graph to compare search intensity over time.
twoway  (tsline PT, lcolor("dkorange") lpattern(solid) lwidth(medthick)) ///
	(tsline EA, lcolor("dknavy") lpattern(solid) lwidth(medthick)) ///
	(tsline Difference, lcolor("gs11") lpattern(solid) lwidth(medthick)), ///
        ytitle("Percentage change", size(medsmall)) ylabel(0(2)10, labsize(medsmall)) ///
	yscale(lcolor(black) lwidth(none) line) xscale(lcolor(black) lwidth(none) line) ///
        tline(3 10, lcolor(gray) lpattern(dash) lwidth(medthin)) ///
        ttitle("") tlabel(1 "Jan-2023" 2 "Feb-2023" 3 "Mar-2023" 4 "Apr-2023" 5 "May-2023" 6 "Jun-2023" 7 "Jul-2023" 8 "Aug-2023" 9 "Sep-2023" 10 "Oct-2023" 11 "Nov-2023" 12 "Dec-2023" 13 "Jan-2024", labsize(medsmall) angle(forty_five) format(%tdMon-CCYY)) ///
        legend(order(1 "Portugal" 2 "Euro area" 3 "Difference") rows(1) pos(6) region(lcolor(black)) size(medsmall)) plotregion(lcolor(black))

// Exporting the graph.
graph export "$additional_figures/figure_a12.png", as(png) replace width(2400) height(1372)

*********************************************************


* Figure A.13: Agricultural Producer Prices from Wholesale Markets
*********************************************************

* Reading all of the csv files.
local files: dir "$additional_data/producer_prices" files "*csv"

cap mkdir "$additional_data/producer_prices/intermediate"

foreach file of local files {
	
	* Load the csv file.
	import delimited "$additional_data/producer_prices/`file'", delimiter(";") encoding(UTF-8) clear 
	di "******* Reading `file'. *******"

	bys produto: gen n = _n
	
	local title = subinstr("`file'", ".csv", "", .)	
	save "$additional_data/producer_prices/intermediate/`title'.dta", replace
}

* Using the bovino csv as a base file.
use   "$additional_data/producer_prices/intermediate/cp01121-bovino", clear 
erase "$additional_data/producer_prices/intermediate/cp01121-bovino.dta"

* Category variable.
gen category = "cp01121-bovino.dta" 

* Appending all the remaining files.
local files: dir "$additional_data/producer_prices/intermediate" files "cp*"

foreach file of local files {
	append using "$additional_data/producer_prices/intermediate/`file'"
	replace category = "`file'" if category == ""
}

* Daily date.
gen day   = substr(data,1,2)
gen month = substr(data,4,2)
gen year  = substr(data,7,4)
destring day month year, replace
gen ddate = mdy(month,day,year)  // Creating a new date variable
format ddate %td
	
* Monthly date.
gen mdate = ym(year,month)
format mdate %tm
	
* Weekly date.
generate wdate = ddate
format wdate %tdCY!ww
	
* Sorting and ordering.     
order category espcie produto  
sort category espcie produto wdate 

* Renaming the variable category.
replace category = "Beef" 			if category == "cp01121-bovino.dta"
replace category = "Pork" 			if category == "cp01122-suino.dta"
replace category = "Sheep and goat" if category == "cp01123-ovino_e_caprino.dta"
replace category = "Poultry" 		if category == "cp01124-aves_capoeira.dta"
replace category = "Eggs" 			if category == "cp01147-ovos.dta"
replace category = "Olive oil" 		if category == "cp01153-azeite.dta"
replace category = "Fresh fruit" 	if category == "cp01161-fruta_fresca.dta"
replace category = "Nuts" 			if category == "cp01163-frutos_secos.dta"
replace category = "Vegetables" 	if category == "cp01171-horticolas_frescos_exceto_batatas.dta"
replace category = "Potatoes" 		if category == "cp01174-batatas.dta"

* Manually creating 2023 COICOP weights.
local sum_wt = 57.15
gen coicop_wt = 0 
replace coicop_wt = 8.26/`sum_wt'  if category == "Beef"
replace coicop_wt = 7.95/`sum_wt'  if category == "Pork"
replace coicop_wt = 1.03/`sum_wt'  if category == "Sheep and goat"
replace coicop_wt = 9.97/`sum_wt'  if category == "Poultry"
replace coicop_wt = 9.97/`sum_wt'  if category == "Eggs" 
replace coicop_wt = 3.24/`sum_wt'  if category == "Olive oil"
replace coicop_wt = 17.42/`sum_wt' if category == "Fresh fruit"
replace coicop_wt = 1.52/`sum_wt'  if category == "Nuts" 
replace coicop_wt = 9.28/`sum_wt'  if category == "Vegetables" 
replace coicop_wt = 3.03/`sum_wt'  if category == "Potatoes" 

* Collapsing.
collapse (mean) freq coicop_wt (min) mnima (max) mxima, by(produto espcie year wdate semana category)
order category espcie produto  
sort category espcie produto wdate 

* Price index.
bys espcie produto: gen price_chg = freq/freq[_n-1]-1
bys espcie produto: gen index = 100 if _n==1
bys espcie produto: replace index = index[_n-1]*(1+price_chg) if _n>1

* Indexing to 2023w12 (20/03).
bys espcie produto: egen value_2023w12 = max(cond(year==2023 & semana==12, index, .))
gen index_2023w12 = 100*index/value_2023w12 

* Dropping if not a lot of observations for each product.
drop if year == 2021
bys category espcie produto year: egen aux = count(freq)
local th = 2/3*52 // 52 = weeks in each year
drop if aux < `th' & year < 2024

* Creating total index.
preserve
drop if coicop_wt == 0 | coicop_wt == . 
collapse (mean) index_2023w12 coicop_wt, by(wdate category)
sort category wdate
bys wdate: egen sum_wt = sum(coicop_wt)
gen total  = 0
bys category wdate: replace total = index_2023w12*coicop_wt/sum_wt
collapse (sum) total, by(wdate)
save "$additional_data/producer_prices/intermediate/ppi_total", replace
restore 

* Creating meat index. 
preserve
keep if category == "Beef" | category == "Pork" | category == "Sheep and goat" | category == "Poultry"
bys wdate: egen sum_wt = sum(coicop_wt)
bys wdate: gen meat = index_2023w12*coicop_wt/sum_wt
collapse (sum) meat, by(wdate)
save "$additional_data/producer_prices/intermediate/ppi_meat", replace
restore

* Creating fruit index. 
preserve
keep if category == "Fresh fruit" 
bys wdate: egen sum_wt = sum(coicop_wt)
bys wdate: gen fruit = index_2023w12*coicop_wt/sum_wt
collapse (sum) fruit, by(wdate)
save "$additional_data/producer_prices/intermediate/ppi_fruit", replace
restore

* Creating vegetable index. 
preserve
keep if category == "Vegetables" 
bys wdate: egen sum_wt = sum(coicop_wt)
bys wdate: gen vegetables = index_2023w12*coicop_wt/sum_wt
collapse (sum) vegetables, by(wdate)
save "$additional_data/producer_prices/intermediate/ppi_vegetables", replace
restore

* By category
egen group = group(category)
order group category espcie produto wdate
sort group category espcie produto wdate 

su group, meanonly
foreach i of num 1/`r(max)' {
	preserve 
	keep if group == `i'
	collapse (mean) index_2023w12, by(category wdate)
	tsset wdate, daily delta(7)
	
	local ann = mdy(3,20,2023)
	local imp = mdy(4,17,2023)
	local arev = mdy(10,27,2023)
	local rev = mdy(1,4,2024)
	local ttl = category[_n]
	di "`ttl'"
		
	restore
}

use "$additional_data/producer_prices/intermediate/ppi_total", clear
merge 1:1 wdate using "$additional_data/producer_prices/intermediate/ppi_meat"
keep if _merge == 3
drop _merge 
merge 1:1 wdate using "$additional_data/producer_prices/intermediate/ppi_fruit"
drop _merge 
merge 1:1 wdate using "$additional_data/producer_prices/intermediate/ppi_vegetables"
drop _merge

drop if total < 50
tsset wdate, delta(7)
gen year = year(wdate)
drop if year == 2022

local ann = mdy(3,20,2023)
local imp = mdy(4,17,2023)
local arev = mdy(10,27,2023)
local rev = mdy(1,4,2024)
local ttl  = "Total"
di "`ttl'"

* Individually
global cat "total meat fruit vegetables"
label var total "Total"
label var meat  "Meat"
label var fruit "Fruit"
label var vegetables "Vegetables"

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 14
grstyle graphsize y 8
grstyle color background white

foreach x of global cat {
	tsset wdate, daily delta(7)
	
	local ann = mdy(3,20,2023)
	local imp = mdy(4,17,2023)
	local arev = mdy(10,27,2023)
	local rev = mdy(1,4,2024)
	local first = mdy(1,1,2023)
	local last = mdy(1,31,2024)
	local label : variable label `x'

	
	tw (tsline `x', color("dknavy")), ///
	xline(`ann', lp(dash) lc(gray) lw(thin)) xline(`imp', lp(dash) lc(gray) lw(thin)) ///
	xline(`arev', lp(dash) lc(gray) lw(thin)) xline(`rev', lp(dash) lc(gray) lw(thin)) ///
	xtitle("") xscale(r(`first'(12)`last') lcolor(black) lwidth(none) line) ///
	xlabel(`first'(84)`last', labsize(vsmall)) ///
	yscale(lcolor(black) lwidth(none) line) ylabel(, labsize(vsmall)) ///
	ytitle("Index (2023w12 = 100)", size(small)) ylabel(,nogrid) ///
	plotregion(lcolor(black) lwidth(medium)) title(`: variable label `x'')
	graph save "$additional_data/producer_prices/intermediate/ppi_`x'_2023.gph", replace
	
}

import excel "$additional_data/producer_prices/leite.xlsx", sheet("Sheet1") firstrow clear

gen date = ym(ano,month)
format date %tm

gen price_chg = leite_prod/leite_prod[_n-1]-1
gen index = 100 if _n==1
replace index = index[_n-1]*(1+price_chg) if _n>1

egen value_2023w12 = max(cond(ano==2023 & month==3, index, .))
gen index_2023w12 = 100*index/value_2023w12 

drop if ano == 2022

tsset date

local ann  = ym(2023,3)
local imp  = ym(2023,4)
local arev = ym(2023,10)
local rev  = ym(2024,1)

tw (tsline index_2023w12, color("dknavy")), ///
xline(`ann', lp(dash) lc(gray) lw(thin)) xline(`imp', lp(dash) lc(gray) lw(thin)) ///
xline(`arev', lp(dash) lc(gray) lw(thin)) xline(`rev', lp(dash) lc(gray) lw(thin)) ///
xlabel(#4, labsize(vsmall)) xtitle("") xscale(lcolor(black) lwidth(none) line) ///
yscale(lcolor(black) lwidth(none) line) ylabel(, labsize(vsmall)) ///
ytitle("Index (2023 March = 100)", size(small)) ///
legend(pos(6) col(4) region(lcolor(black)) size(medsmall)) ylabel(,nogrid) ///
plotregion(lcolor(black) lwidth(medium)) title("Milk")

graph save "$additional_data/producer_prices/intermediate/ppi_milk_2023.gph", replace

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 15
grstyle graphsize y 20
grstyle color background white

graph combine ///
    "$additional_data/producer_prices/intermediate/ppi_total_2023.gph" ///
    "$additional_data/producer_prices/intermediate/ppi_meat_2023.gph" ///
    "$additional_data/producer_prices/intermediate/ppi_fruit_2023.gph" ///
    "$additional_data/producer_prices/intermediate/ppi_vegetables_2023.gph" ///
    "$additional_data/producer_prices/intermediate/ppi_milk_2023.gph", cols(2)

graph export "$additional_figures/figure_a13.png", as(png) replace width(2400) height(1372)

erase "$additional_data/producer_prices/intermediate/ppi_total_2023.gph"
erase "$additional_data/producer_prices/intermediate/ppi_meat_2023.gph"
erase "$additional_data/producer_prices/intermediate/ppi_fruit_2023.gph"
erase "$additional_data/producer_prices/intermediate/ppi_vegetables_2023.gph"
erase "$additional_data/producer_prices/intermediate/ppi_milk_2023.gph"

erase "$additional_data/producer_prices/intermediate/ppi_total.dta"
erase "$additional_data/producer_prices/intermediate/ppi_meat.dta"
erase "$additional_data/producer_prices/intermediate/ppi_fruit.dta"
erase "$additional_data/producer_prices/intermediate/ppi_vegetables.dta"

*********************************************************


* Figure A.14: Industrial Production Price Index for Manufacturing Food Industries
*********************************************************

* Loading the dataset.
import excel "$additional_data/food_industrial_prices.xlsx", sheet("base_2021") firstrow clear

* Date variable.
gen year = year(date)
gen month = month(date)
drop date
gen date = ym(year,month)
format date %tm
di ym(2023,4)
order date year month

* Renaming.
global series "C101Abatedeanimaisprepara C102Preparaçãoeconservação C103Preparaçãoeconservação C104Produçãodeóleosegordu C105Indústriadelacticínios C106Transformaçãodecereais C107Fabricaçãodeprodutosde C108Fabricaçãodeoutrosprod"
rename C10Indústriasalimentares C10
rename C11Indústriadasbebidas   C11
foreach x of global series {
	local newname = substr("`x'",1,4)
	di "`newname'"
	rename `x' `newname'
}

* Changing the base from 2015 to 2023m3.
global series "C10 C101 C102 C103 C104 C105 C106 C107 C108 C11"
foreach x of global series {
	cap drop aux
	gen aux = `x' if year == 2023 & month == 3
	egen index_`x'_2023m3 = max(cond(year == 2023 & month == 3, aux, .))
	gen  new_`x'_2023m3 = `x'/index_`x'_2023m3*100 
}
drop index* aux

* Declaring as time series.
tsset date 

* Keeping only 2022 onwards.
keep if year >= 2023

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 14
grstyle graphsize y 8
grstyle color background white

tw (tsline new_C10_2023m3,  lcolor(dknavy)) ///
   (tsline new_C101_2023m3, lcolor(dkorange)) ///
   (tsline new_C102_2023m3, lcolor(gray)) ///
   (tsline new_C103_2023m3, lcolor(dkgreen)), ///
   xline(758, lp(dash) lc(gray) lw(thin)) ///
   xline(759, lp(dash) lc(gray) lw(thin)) ///
   xline(765, lp(dash) lc(gray) lw(thin)) ///
   xline(768, lp(dash) lc(gray) lw(thin)) ///
   ytitle("Index (March 2023 = 100)") ///
   legend(pos(6) rows(1) label(1 "Total") label(2 "Meat") label(3 "Fish") ///
		  label(4 "Fruit and vegetables") region(lcolor(black)) size(medsmall)) plotregion(lcolor(black))  ylabel(,nogrid) ///
   plotregion(lcolor(black) lwidth(medium)) xtitle("")

graph export "$additional_figures/figure_a14.png", as(png) replace width(2400) height(1372)

*********************************************************


* FIGURE C.1: Price Evolution of the Basket of Treated Items in Portugal and Spain
*********************************************************

// Import COICOP 5 list of treated products.
import excel  "$additional_data/coicop_weights.xlsx", sheet("weights") firstrow clear

// Keep only food categories.
keep if food == 1

* Save the list of treated products.
save "$additional_data/food_list.dta", replace

* Loading the price microdata from Eurostat.
import delimited "$additional_data/estat_prc_hicp_midx_en.csv", clear

* Date variables.
gen year  = substr(time_period,1,4)
gen month = substr(time_period,6,2)
destring year month, replace
gen date = ym(year,month)
format %tm date

* Restrictions.
keep if geo == "PT" | geo == "ES"
keep if year >= 2019
keep if unit == "I15"
encode geo, gen(country)

* Merge to get VAT 0 products.
rename coicop code
merge m:1 code using "$additional_data/food_list.dta"
keep if _merge == 3
drop dataflow lastupdate unit obs_flag _merge

replace iva_zero = 0  if iva_zero ==.

* Arrange data.
sort code country date  
tab date

* Something seems to be available for PT but not ES -> drop.
drop if code == "CP01152" | code == "CP01143" // Margarina e leite conservado
cap drop tot_wt
bys country date: egen tot_wt = sum(weight_2023)

* New weights.
gen new_wt = weight_2023/tot_wt
bys country date: egen new_tot_wt = sum(new_wt) // Testing

* Aggregate price series for treated items.
gen aux = obs_value*new_wt
collapse (sum) aux, by(country date year month)

* Rebasing to 2023m4.
bys country date: gen base1 = aux if year == 2023 & month == 4
bys country: egen base2 = max(base1)
gen index_2023m4 = aux/base2*100
drop base*

* Plot with new base.
keep if date >= tm(2023m1)
keep if date <= tm(2023m12)
cap decode country, gen(geo)
xtset country date, monthly

twoway ///
    (tsline index_2023m4 if geo=="PT", lcolor("dkorange") lpattern(solid) lwidth(medthin)) ///
    (tsline index_2023m4 if geo=="ES", lcolor("dknavy") lpattern(solid) lwidth(medthin)), ///
    xscale(lcolor(black) lwidth(none) line) yscale(lwidth(none) line) ylabel(, labsize(medsmall)) ///
    ytitle("Index (Apr-2023 = 100)", size(medsmall)) xtitle("") ///
    tlabel(2023m1 "Jan-2023" 2023m2 "Feb-2023" 2023m3 "Mar-2023" 2023m4 "Apr-2023" 2023m5 "May-2023" ///
    2023m6 "Jun-2023" 2023m7 "Jul-2023" 2023m8 "Aug-2023" 2023m9 "Sep-2023" 2023m10 "Oct-2023" ///
    2023m11 "Nov-2023" 2023m12 "Dec-2023", labsize(medsmall) angle(forty_five) format(%tdMon_YY)) ///
    xline(759, lp(dash) lc(black) lw(medthin)) plotregion(lcolor(black) lwidth(medium)) plotregion(lcolor(black)) ///
    legend(label(1 "Portugal") label(2 "Spain") position(6) rows(1) region(lcolor(black)) size(medsmall)) ylabel(,nogrid)
	
graph export "$additional_figures/figure_c1.png", as(png) replace width(2400) height(1372)

erase "$additional_data/food_list.dta"

*********************************************************


* TABLE C.1: Robustness: Alternative Identification Strategy
*********************************************************

// Import COICOP 5 list of treated products.
import excel  "$additional_data/coicop_weights.xlsx", sheet("weights") firstrow clear

// Keep only food categories.
keep if food == 1

* Save the list of treated products.
save "$additional_data/food_list.dta", replace

* Loading the price microdata from Eurostat.
import delimited "$additional_data/estat_prc_hicp_midx_en.csv", clear

* Date variables.
gen year  = substr(time_period,1,4)
gen month = substr(time_period,6,2)
destring year month, replace
gen date = ym(year,month)
format %tm date

* Restrictions.
keep if geo == "PT" | geo == "ES"
keep if year >= 2019
keep if unit == "I15"
encode geo, gen(country)

* Merge to get VAT 0 products.
rename coicop code
merge m:1 code using "$additional_data/food_list.dta"
keep if _merge == 3
drop dataflow lastupdate unit obs_flag _merge

replace iva_zero = 0  if iva_zero ==.

* Arrange data.
gen coicop = substr(code,3,7)
destring coicop, replace

* Generate time treatment dummy variable
gen time_treat = 0
replace time_treat = 1 if date > tm(2023m4)

* DiD regression.
drop if date < tm(2023m1) //that's when Spain can be taken as a clean control.
drop if date > tm(2023m12)
eststo clear

eststo, title("(1)"): reg obs_val country, vce(cluster coicop)

eststo, title("(2)"): reg obs_val time_treat, vce(cluster coicop)

eststo, title("(3)"): reg obs_val i.country##c.time_treat, vce(cluster coicop)

label var time_treat "T_t"
label var country "C_i"

estout *, cells(b se(par)) stats(r2 N)
estout * using "$additional_tables/table_c1.txt", cells(b se(par)) stats(r2 N) replace

erase "$additional_data/food_list.dta"

*********************************************************


clear all

cd "$additional_data/producer_prices/intermediate"
local files: dir . files "*.dta"

foreach file of local files {
    rm "`file'"
}

*********************************************************
